﻿using Cookbook.Models;
using Cookbook.Services;
using Microsoft.Practices.Prism.StoreApps;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cookbook.ViewModels
{
  public class RecipesViewModel : ViewModel
  {
    private ObservableCollection<Recipe> recipes;

    public ObservableCollection<Recipe> Recipes
    {
      get { return recipes; }
      set { this.SetProperty(ref this.recipes, value); }
    }

    private IEnumerable<Recipe> topRecipes;

    public IEnumerable<Recipe> TopRecipes
    {
      get { return topRecipes; }
      set { this.SetProperty(ref this.topRecipes, value); }
    }
    
    private Recipe recipeOfTheDay;

    public Recipe RecipeOfTheDay
    {
      get { return recipeOfTheDay; }
      set { this.SetProperty(ref this.recipeOfTheDay, value); }
    }

    public RecipesViewModel()
    {
      this.Recipes = new ObservableCollection<Recipe>();
    }

    public async Task LoadRecipes()
    {
      var recipes = await RecipeService.GetRecipes();
      this.Recipes = new ObservableCollection<Recipe>(recipes);

      // new: set top recipes and recipe of the day
      this.TopRecipes = recipes.GroupBy(p => p.Rating).OrderByDescending(p => p.Key).First();
      this.RecipeOfTheDay = this.TopRecipes.First();
    }
  }
}
